const fs = require('fs');

const tableHeader = `
# Table of Contents

| 序号 | 文件位置 | 说明 | 时间 |
| - | - | - | - |`;

const fileNames = fs.readdirSync('./javascript', { encoding: 'utf-8' });

const parseFile = (text) => {
    const briefReg = /(?<=\* @brief\s+)(.*)(?=\n)/g;
    const dateReg = /(?<=\* @date\s+)(.*)(?=\n)/g;
    const get = (reg) =>
        text
            .match(reg)
            ?.map((s) => s.trim())
            .join(' , ') ?? '-';
    return {
        brief: get(briefReg),
        date: get(dateReg),
    };
};

const tableContent = fileNames.map((f, i) => {
    const filePath = './javascript/' + f;
    const fileContent = fs.readFileSync(filePath, { encoding: 'utf-8' });
    const { brief, date } = parseFile(fileContent);
    return `
| ${i} | [${f}](${filePath}) | ${brief} | ${date} |`;
});

fs.writeFileSync('README.md', tableHeader + tableContent.join(''));
